package com.haiyou.dc.thread.test1;

//---->5
//---->5
//---->4
//---->3
//---->2
//---->1
//---->-1
//---->0

/**
 * 线程不安全：数据有负数、有重复
 */
public class UnsafeTest01 {

    public static void main(String[] args) {
        UnsafeWeb12306 unsafeWeb12306 = new UnsafeWeb12306();

        new Thread(unsafeWeb12306, "AAAA").start();
        new Thread(unsafeWeb12306, "BBBB").start();
        new Thread(unsafeWeb12306, "CCCC").start();
    }

}

class UnsafeWeb12306 implements Runnable {

    private Integer ticketNum = 5;
    private Boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            buy();
        }
    }

    public void buy() {
        if (ticketNum <= 0) {
            flag = false;
            return;
        }
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("---->" + ticketNum--);
    }
}
